package com.huanshi.module.ums.controller;


import com.huanshi.common.CommonResult;
import com.huanshi.entity.UmsAddress;
import com.huanshi.service.UmsAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 幻世
 * @since 2023-10-10
 */
@RestController
@RequestMapping("/api/ums/address")
public class UmsAddressController {

    @Resource
    private UmsAddressService addressService;

    // 新增或者更新
    @PostMapping
    public CommonResult save(@RequestBody UmsAddress umsAddress) {
        return CommonResult.success(addressService.saveOrUpdate(umsAddress));
    }
    @GetMapping("/update-flag")
    public CommonResult save(@RequestParam("addressId") Long addressId, @RequestParam("userId") Long userId) {
        return CommonResult.success(addressService.updateFlag(addressId, userId));
    }

    @DeleteMapping("/{id}")
    public CommonResult delete(@PathVariable Long id) {
        return CommonResult.success(addressService.removeById(id));
    }

    @PostMapping("/del/batch")
    public CommonResult deleteBatch(@RequestBody List<Integer> ids) {
        return CommonResult.success(addressService.removeByIds(ids));
    }

    @GetMapping
    public CommonResult findAll() {
        return CommonResult.success(addressService.list());
    }

    @GetMapping("/{id}")
    public CommonResult findOne(@PathVariable Long id) {
        return CommonResult.success(addressService.getById(id));
    }
    @GetMapping("/default/{userId}")
    public CommonResult findDefaultAddress(@PathVariable Long userId) {
        return CommonResult.success(addressService.getDefaultAddress(userId));
    }

    @GetMapping("/user/{userId}")
    public CommonResult findOneByUserId(@PathVariable Integer userId) {
        return CommonResult.success(addressService.getByUserId(userId));
    }

}